//
//  ADSuyiCustomAdapterTestUnifiedNativeAd.m
//  ADSuyiCustomPlatform
//
//  Created by Erik on 2021/9/8.
//

#import "ADSuyiCustomAdapterTestUnifiedNativeAd.h"
#import <GDTMobSDK/GDTUnifiedNativeAd.h>
#import <GDTMobSDK/GDTUnifiedNativeAdView.h>
#import <ADSuyiSDK/ADSuyiAdapterNativeAdData.h>
#import "ADSuyiCustomAdapterTestUnifiedNativeAdView.h"

@interface ADSuyiCustomAdapterTestUnifiedNativeAd ()<GDTUnifiedNativeAdDelegate,ADSuyiCustomAdapterUnifiedNativeAdViewDelegate,GDTUnifiedNativeAdViewDelegate>
{
    ADSuyiCustomAdapterRequestContext *_context;
    
    GDTUnifiedNativeAd *_unifiedNativeAdAd;
    
    NSMapTable<GDTUnifiedNativeAdDataObject *,ADSuyiCustomAdapterTestUnifiedNativeAdView *> *_weakMap;
}
@end

@implementation ADSuyiCustomAdapterTestUnifiedNativeAd

+ (void)load {
    [self registPlatformAdLoaderClass:self forSdkName:@"gdt" renderType:(ADSuyiAdapterRenderTypeNative)];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _weakMap = [NSMapTable weakToWeakObjectsMapTable];
    }
    return self;
}

- (void)requestAdWithContext:(ADSuyiCustomAdapterRequestContext *)context {
    _context = context;
    [self.unifiedNativeAdAd loadAdWithAdCount:_context.count];
}


- (NSArray<ADSuyiCustomAdapterTestUnifiedNativeAdView *> *)createAdViewArrayFromDataArray:(NSArray<__kindof GDTUnifiedNativeAdDataObject *> *)dataArray {
    NSMutableArray *nativeAdArray = [NSMutableArray new];
    for (GDTUnifiedNativeAdDataObject *dataObj in dataArray) {
        ADSuyiCustomAdapterTestUnifiedNativeAdView *adView = [ADSuyiCustomAdapterTestUnifiedNativeAdView new];
        adView.adData = dataObj;
        [nativeAdArray addObject:adView];
    }
    return nativeAdArray.copy;
}

#pragma mark - GDTUnifiedNativeAdDelegate

- (void)gdt_unifiedNativeAdLoaded:(NSArray<GDTUnifiedNativeAdDataObject *> * _Nullable)unifiedNativeAdDataObjects error:(NSError * _Nullable)error {
    if(error) {
        [self trackNativeAdFailToLoadWithError:error];
        return;
    }
    NSArray<ADSuyiCustomAdapterTestUnifiedNativeAdView *> *adViewArray = [self createAdViewArrayFromDataArray:unifiedNativeAdDataObjects];
    NSInteger i = 0;
    for (ADSuyiCustomAdapterTestUnifiedNativeAdView *adView in adViewArray) {
        adView.adView.delegate = self;
        adView.delegate = self;
        adView.adView.viewController = _context.viewController;
        [_weakMap setObject:adView forKey:unifiedNativeAdDataObjects[i]];
        adView.adData.videoConfig.videoMuted = YES;
        i++;
    }
    [self trackNativeAdSucceedToLoadWithNativeAdViews:adViewArray];
}

#pragma mark - GDTUnifiedNativeAdViewDelegate

- (void)gdt_unifiedNativeAdViewWillExpose:(GDTUnifiedNativeAdView *)unifiedNativeAdView {
    ADSuyiCustomAdapterTestUnifiedNativeAdView *adView = [_weakMap objectForKey:unifiedNativeAdView.dataObject];
    [self trackNativeAdExposuredWithNativeView:adView];
}

- (void)gdt_unifiedNativeAdViewDidClick:(GDTUnifiedNativeAdView *)unifiedNativeAdView {
    ADSuyiCustomAdapterTestUnifiedNativeAdView *adView = [_weakMap objectForKey:unifiedNativeAdView.dataObject];
    [self trackNativeAdClickedWithNativeView:adView];
}

#pragma mark - ADSuyiCustomAdapterUnifiedNativeAdViewDelegate

- (void)adsyCustomUnifiedNativeAdViewRender:(ADSuyiCustomAdapterUnifiedNativeAdView *)adView {
    [self trackNativeAdRenderSucceedWithNativeView:adView];
}

- (void)adsyCustomUnifiedNativeAdViewClose:(ADSuyiCustomAdapterUnifiedNativeAdView *)adView {
    [self trackNativeAdClosedWithNativeView:adView];
}



#pragma mark - Lazy load

- (GDTUnifiedNativeAd *)unifiedNativeAdAd {
    if(!_unifiedNativeAdAd) {
        _unifiedNativeAdAd = [[GDTUnifiedNativeAd alloc] initWithPlacementId:_context.posId];
        _unifiedNativeAdAd.delegate = self;
    }
    return _unifiedNativeAdAd;
}

@end
